package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.ov.CoursePublishVo;
import com.atguigu.eduservice.entity.ov.CourseQuery;
import com.atguigu.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-08-03
 */
@RestController
@RequestMapping("/eduservice/eduCourse")
//@CrossOrigin
public class EduCourseController {

    @Autowired
    EduCourseService eduCourseService;


    @PostMapping("/getCoursePage/{current}/{limit}")
    public R getCoursePage(@PathVariable int current,@PathVariable int limit,
                           @RequestBody(required = false) EduCourse eduCourse){
        Map map=eduCourseService.getCoursePage(current,limit,eduCourse);
        return R.ok().data("list",map.get("records")).data("total",map.get("total"));
    }

    @PostMapping("addCourse")
    public R addCourse(@RequestBody CourseQuery courseQuery){
        String id=eduCourseService.saveCourse(courseQuery);
        return R.ok().data("courseId",id);
    }

    @GetMapping("/getCourseInfoId/{courseId}")
    public R getCourseInfoId(@PathVariable String courseId){
        CourseQuery courseQuery = eduCourseService.getCourseInfo(courseId);
        return R.ok().data("courseQuery",courseQuery);
    }

    @PostMapping("updateCourse")
    public R updateCourse(@RequestBody CourseQuery courseQuery){
        eduCourseService.updateCourseDescription(courseQuery);
        return R.ok();
    }

    //根据课程id查询课程的确认信息
    @GetMapping("/getPublishCourseInfo/{id}")
    public R getPublishCourseInfo(@PathVariable String id){
        CoursePublishVo oursePublishVo=eduCourseService.PublishCourseInfo(id);
        return R.ok().data("oursePublishVo",oursePublishVo);
    }

    //修改课程为以发布状态
    @PostMapping("/updatePublisStatus/{id}")
    public R updatePublisStatus(@PathVariable String id){
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");
        eduCourseService.updateById(eduCourse);
        return R.ok();
    }

    @DeleteMapping("/deleteCourseId/{courseId}")
    public R deleteCourseId(@PathVariable String courseId){
        eduCourseService.deleteCourseId(courseId);
        return R.ok();
    }
}

